function [value]=cal_likelihood(model, data)

%global model;

N      = size(data.dataw1(1).w,1);
k      = data.k;
k_hat  = data.k_hat;
rhs_on = 1;

%% terms for LHS part

term11 = -(N/2)*sum(log(2*pi*(model.sigma_01.^2)));
term12 = -sum(sum((model.sigma_1.^2)+((repmat(model.alpha_01,N,1)-model.alpha_1).^2),1)./(2*(model.sigma_01.^2)));
term1  = term11+term12;

term2  = -(1/2)*sum(sum(log(2*pi*(model.sigma_1.^2)))) - N*k/2;
term2  = -term2;

term41 = 0;
for n=1:N
    for l=1:model.r1
        term41 = term41 + model.alpha_1(n,:)*[data.dataw1(l).w(n,:)]';
    end
end
term42 = -(model.r1./model.zeta_1)*sum(exp(model.alpha_1+(model.sigma_1.^2)/2),2);
term43 = N*model.r1;
term44 = -model.r1*sum(log(model.zeta_1));
term4  = term41+term42+term43+term44;

value  = term1+term2+term4;


%%% terms for RHS part starts

if(rhs_on==1)

term31 = (-N/2)*(sum(log(2*pi*model.sigma_02.^2)));
term32 = -sum(sum(model.sigma_2.^2+(repmat(model.alpha_02,N,1)-model.alpha_2).^2,1)./(2*model.sigma_02.^2));
term33 = sum([-1./(2*model.sigma_02.^2)].*[(model.eta.^2)*(sum((model.sigma_1.^2+model.alpha_1.^2),1))']');
term33 = term33*(model.lambda^2);


term34 = zeros(1,k_hat);

   
  for m=2:k
     for l=1:(m-1)
        term34 = term34 - sum(model.alpha_1(:,l).*model.alpha_1(:,m))*[model.eta(:,l).*model.eta(:,m)]'./(model.sigma_02.^2);
     end
  end

term34 = sum(term34)*(model.lambda^2);  
  
term35 = sum(sum([(model.alpha_2-repmat(model.alpha_02,N,1))./repmat(model.sigma_02.^2,N,1)].*[model.eta*model.alpha_1']'));
term35 = term35*model.lambda;

term3  = term31+term32+term33+term34+term35;


term5  = (-1/2)*sum(sum(log(2*pi*model.sigma_2.^2))) - N*k_hat/2;
term5  = -term5;


term71 = model.phi_2.*log(model.phi_2);
ind    = [];
ind    = find(isnan(term71)==1);
term71(ind) = 0;
term7  = sum(sum(sum(term71)));
term7  = -term7;

term81 = sum(sum(reshape(sum(model.phi_2,2),N,k_hat).*model.alpha_2));
term82 = -(model.r2./model.zeta_2)*sum(exp(model.alpha_2+model.sigma_2.^2/2),2);
term83 = N*model.r2;
term84 = -model.r2*sum(log(model.zeta_2));
term8  = term81+term82+term83+term84;


term10  = 0;
for r=1:model.r2
 tempw2     = data.dataw2(r).w;
 tempphi_2  = reshape(model.phi_2(:,r,:),N,k_hat);
 for j=1:k_hat
  term10 = term10 + tempphi_2(:,j).*(tempw2*(log(model.beta_2(r,j).beta))');
 end
end
term10 = sum(term10);


valuetemp = term3+term5+term7+term8+term10;
value     = value + valuetemp;

%%% terms for RHS part ends
end

end
